import { getTicket } from "@/utils/common"
import dd from "gdt-jsapi"

class Jsapi {
	loadfinish: boolean = false
	readyFn: Function = null
	
	constructor() {
		this.init()
	}
	
	public init() {
		getTicket()
			.then((res) => {
				dd.authConfig({
					ticket: res.ticket,
					jsApiList: ["showOnMap", "startGeolocation", "stopGeolocation", "getGeolocation"]
				}).then(() => {
					this.readyFn()
					this.loadfinish = true
					
				}).catch((err: any) => {
					uni.showToast({
						icon: "none",
						title: "获取JSAPI鉴权失败" + JSON.stringify(err)
					})
				})
			})
			.catch((err) => {
				uni.showToast({
					icon: "none",
					title: "获取TICKET失败"
				})
			})
	}
}

const jsapi = new Jsapi()

export function authReady(fn: Function) {
	if (jsapi.loadfinish) {
		fn()
	} else {
		jsapi.readyFn = fn
	}
}

